﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//Author: ljj
//Function: aoe攻击
public class AoeAttack : BaseAttack
{
    private Vector3 target;
    private float range;
    private Identity identity;

    public AoeAttack(GameObject attacker, Vector3 target, float range, float damage, Identity identity)
    {
        this.attacker = attacker;
        this.target = target;
        this.range = range;
        this.damage = damage;
        this.identity = identity;
    }

    public override void Attack()
    {
        Collider2D[] cols = Physics2D.OverlapCircleAll(target, range, Function.GetTargetLayerMask(identity)); 
        int count = 0;
        if (cols.Length > 0)
        {
            for (int i = 0; i < cols.Length; i++)
            {
                BattleObject targetBattle = cols[i].GetComponent<BattleObject>();
                if (targetBattle != null &&
                    targetBattle.id != identity)
                {
                    ++count;
                    if (buffAdderOnAttack != null)
                        buffAdderOnAttack.AddBuffToObj(targetBattle.gameObject);
                    targetBattle.attacked(damage);
                    if (attacker != null)
                    {
                        BattleObject battle = attacker.GetComponent<BattleObject>();
                        if (battle != null)
                            battle.addDamageScore(damage);
                    }
                }
            }
        }
    }

    public void setTarget(Vector3 target)
    {
        this.target = target; 
    }

    public override Vector3 GetTargetPos()
    {
        return target;
    }
}
